﻿Imports System.Text.RegularExpressions

Public Class currensyConverterUC
    Dim imageBoxName As PictureBox
    Dim combName As ComboBox
    Dim currensyTable(0 To 10, 0 To 10) As Double

    Private Sub converter1ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles converter1ComboBox.SelectedIndexChanged, converter2ComboBox.SelectedIndexChanged, converter3ComboBox.SelectedIndexChanged

        ' η μεθοδος για να μην μπορει να επιλεγεται το ιδιο νομισμα 2η φορα
        ' και για να αλλαζουν οι σημαιες
        If sender.Name = "converter1ComboBox" Then
            imageBoxName = flagBox1
            If Not sender.selectedIndex = -1 Then
                If similarCurrensyCheckBox.Checked = False Then
                    If sender.selectedIndex = converter2ComboBox.SelectedIndex Or sender.selectedIndex = converter3ComboBox.SelectedIndex Then

                        showError(sender)
                        ' αν ειναι αποεπιλεγμενη η επιλογη του CheckBox
                        ' να εμφανιζεται το λαθος
                        Return
                    End If
                End If

                imageBoxName.Visible = True
                updateImages(sender)
            End If
        ElseIf sender.Name = "converter2ComboBox" Then
            imageBoxName = flagBox2
            If Not sender.selectedIndex = -1 Then
                If similarCurrensyCheckBox.Checked = False Then
                    If sender.selectedIndex = converter1ComboBox.SelectedIndex Or sender.selectedIndex = converter3ComboBox.SelectedIndex Then

                        showError(sender)
                        ' αν ειναι αποεπιλεγμενη η επιλογη του CheckBox
                        ' να εμφανιζεται το λαθος
                        Return
                    End If
                End If

                imageBoxName.Visible = True
                updateImages(sender)
            End If
        ElseIf sender.Name = "converter3ComboBox" Then
            imageBoxName = flagBox3
            If Not sender.selectedIndex = -1 Then
                If similarCurrensyCheckBox.Checked = False Then
                    If sender.selectedIndex = converter1ComboBox.SelectedIndex Or sender.selectedIndex = converter2ComboBox.SelectedIndex Then

                        showError(sender)
                        ' αν ειναι αποεπιλεγμενη η επιλογη του CheckBox
                        ' να εμφανιζεται το λαθος
                        Return
                    End If
                End If

                imageBoxName.Visible = True
                updateImages(sender)
            End If
        End If




        ' η μεθοδος υπολογισμου των ισοτιμιων
        If converterTextBox1.Enabled = True And converterTextBox2.Enabled = True _
                And converterTextBox3.Enabled = True Then

        Else
            If converterTextBox1.Enabled = True Then
                checkComboBoxEnabled(converter1ComboBox)
            ElseIf converterTextBox2.Enabled = True Then
                checkComboBoxEnabled(converter2ComboBox)
            ElseIf converterTextBox3.Enabled = True Then
                checkComboBoxEnabled(converter3ComboBox)
            End If
        End If
    End Sub

    Private Sub currensyConverterUC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        flagBox1.Visible = False
        flagBox2.Visible = False
        flagBox3.Visible = False
        resetBtn.Visible = False

        fillCurrensyTable()
    End Sub

    Private Sub flagBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        flagBox1.MouseEnter, flagBox2.MouseEnter, flagBox3.MouseEnter, converter1ComboBox.MouseEnter, converter2ComboBox.MouseEnter, converter3ComboBox.MouseEnter

        If sender.name.contains("1") Then
            comboBoxItem(sender, 1)
        ElseIf sender.name.contains("2") Then
            comboBoxItem(sender, 2)
        ElseIf sender.name.contains("3") Then
            comboBoxItem(sender, 3)
        End If
    End Sub

    Public Sub comboBoxItem(ByVal sender As System.Object, ByVal number As Integer)
        If number = 1 Then
            combName = converter1ComboBox
        ElseIf number = 2 Then
            combName = converter2ComboBox
        ElseIf number = 3 Then
            combName = converter3ComboBox
        End If

        If combName.SelectedItem = "EUR - € -" Then
            myToolTip.SetToolTip(sender, "Euro")
        ElseIf combName.SelectedItem = "USD - $ -" Then
            myToolTip.SetToolTip(sender, "United States Dollar")
        ElseIf combName.SelectedItem = "GBP - £ -" Then
            myToolTip.SetToolTip(sender, "Great Britain Pound")
        ElseIf combName.SelectedItem = "JPY - ¥ -" Then
            myToolTip.SetToolTip(sender, "Japanese Yen")
        ElseIf combName.SelectedItem = "AUD - $ -" Then
            myToolTip.SetToolTip(sender, "Australian dollar")
        ElseIf combName.SelectedItem = "BGN - лв -" Then
            myToolTip.SetToolTip(sender, "Bulgarian Lev")
        ElseIf combName.SelectedItem = "DKK - kr -" Then
            myToolTip.SetToolTip(sender, "Danish Krone")
        ElseIf combName.SelectedItem = "CHF - Fr -" Then
            myToolTip.SetToolTip(sender, "Swiss franc")
        ElseIf combName.SelectedItem = "TRY - TL -" Then
            myToolTip.SetToolTip(sender, "Turkish Lira")
        ElseIf combName.SelectedItem = "ARS - $ -" Then
            myToolTip.SetToolTip(sender, "Argentine Peso")
        End If
    End Sub

    Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click
        Me.ParentForm.Close()
    End Sub

    Public Sub currensyConverterValidating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim generalPhoneNumRegExp As Regex = New Regex("^[1-9][0-9]{0,9}$")

        If sender.Text = String.Empty Then
            onlyNumbersErProvider.SetError(sender, String.Empty)
        Else
            If (Not IsNumeric(sender.Text)) Then
                onlyNumbersErProvider.SetError(sender, "Το πεδίο πρέπει να περιέχει μόνο αριθμους...")
                e.Cancel = True
            ElseIf (IsNumeric(sender.Text)) Then
                If (Not generalPhoneNumRegExp.IsMatch(sender.Text)) Then
                    onlyNumbersErProvider.SetError(sender, "Το πεδίο πρέπει να περιέχει έως 10 αριθμους" & vbCrLf & "και να μην ξεκινάει από 0...")
                    e.Cancel = True
                Else
                    onlyNumbersErProvider.SetError(sender, String.Empty)
                End If
            End If
        End If
    End Sub

    Private Sub converterTextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles converterTextBox3.Validating, converterTextBox2.Validating, converterTextBox1.Validating
        currensyConverterValidating(sender, e)
    End Sub

    Private Sub converterTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles converterTextBox1.TextChanged, converterTextBox2.TextChanged, converterTextBox3.TextChanged
        If sender.enabled = True Then
            If sender.name = "converterTextBox1" Then
                converterTextBox2.Enabled = False
                converterTextBox3.Enabled = False

                checkComboBoxEnabled(converter1ComboBox)
            ElseIf sender.name = "converterTextBox2" Then
                converterTextBox1.Enabled = False
                converterTextBox3.Enabled = False

                checkComboBoxEnabled(converter2ComboBox)
            ElseIf sender.name = "converterTextBox3" Then
                converterTextBox1.Enabled = False
                converterTextBox2.Enabled = False

                checkComboBoxEnabled(converter3ComboBox)
            End If
        End If
    End Sub

    Public Sub checkComboBoxEnabled(ByVal activeComboBox As System.Object)
        If activeComboBox.Name = "converter1ComboBox" Then
            If Not converter2ComboBox.SelectedIndex = -1 Then
                converterTextBox2.Text = (converterTextBox1.Text * currensyTable(activeComboBox.SelectedIndex + 1, converter2ComboBox.SelectedIndex + 1))
            End If
            If Not converter3ComboBox.SelectedIndex = -1 Then
                converterTextBox3.Text = (converterTextBox1.Text * currensyTable(activeComboBox.SelectedIndex + 1, converter3ComboBox.SelectedIndex + 1))
            End If
        ElseIf activeComboBox.Name = "converter2ComboBox" Then
            If Not converter1ComboBox.SelectedIndex = -1 Then
                converterTextBox1.Text = (converterTextBox2.Text * currensyTable(activeComboBox.SelectedIndex + 1, converter1ComboBox.SelectedIndex + 1))
            End If
            If Not converter3ComboBox.SelectedIndex = -1 Then
                converterTextBox3.Text = (converterTextBox2.Text * currensyTable(activeComboBox.SelectedIndex + 1, converter3ComboBox.SelectedIndex + 1))
            End If

        ElseIf activeComboBox.Name = "converter3ComboBox" Then
            If Not converter1ComboBox.SelectedIndex = -1 Then
                converterTextBox1.Text = (converterTextBox3.Text * currensyTable(activeComboBox.SelectedIndex + 1, converter1ComboBox.SelectedIndex + 1))
            End If
            If Not converter3ComboBox.SelectedIndex = -1 Then
                converterTextBox2.Text = (converterTextBox3.Text * currensyTable(activeComboBox.SelectedIndex + 1, converter2ComboBox.SelectedIndex + 1))
            End If
        End If
        resetBtn.Visible = True
    End Sub

    Private Sub resetBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resetBtn.Click
        converter1ComboBox.SelectedIndex = -1
        converter2ComboBox.SelectedIndex = -1
        converter3ComboBox.SelectedIndex = -1
        flagBox1.Visible = False
        flagBox2.Visible = False
        flagBox3.Visible = False

        resetBtn.Visible = False
        
        converterTextBox1.Text = ""
        converterTextBox2.Text = ""
        converterTextBox3.Text = ""

        converterTextBox1.Enabled = True
        converterTextBox2.Enabled = True
        converterTextBox3.Enabled = True
    End Sub

    Public Sub updateImages(ByVal sender As System.Object)
        If sender.SelectedItem = "EUR - € -" Then
            imageBoxName.Image = My.Resources.eu
        ElseIf sender.SelectedItem = "USD - $ -" Then
            imageBoxName.Image = My.Resources.usa
        ElseIf sender.SelectedItem = "GBP - £ -" Then
            imageBoxName.Image = My.Resources.england
        ElseIf sender.SelectedItem = "JPY - ¥ -" Then
            imageBoxName.Image = My.Resources.japan
        ElseIf sender.SelectedItem = "AUD - $ -" Then
            imageBoxName.Image = My.Resources.australia
        ElseIf sender.SelectedItem = "BGN - лв -" Then
            imageBoxName.Image = My.Resources.bulgaria
        ElseIf sender.SelectedItem = "DKK - kr -" Then
            imageBoxName.Image = My.Resources.denmark
        ElseIf sender.SelectedItem = "CHF - Fr -" Then
            imageBoxName.Image = My.Resources.swiss
        ElseIf sender.SelectedItem = "TRY - TL -" Then
            imageBoxName.Image = My.Resources.turkey
        ElseIf sender.SelectedItem = "ARS - $ -" Then
            imageBoxName.Image = My.Resources.argentina
        End If
    End Sub

    Public Sub showError(ByVal sender As System.Object)
        MessageBox.Show("Δεν επιτρέπεται το ίδιο νόμισμα 2η φόρα...", _
                                    "Currensy Converter", MessageBoxButtons.OK, MessageBoxIcon.Error)

        If sender.name = "converter1ComboBox" Then
            converterTextBox1.Text = ""
        ElseIf sender.name = "converter2ComboBox" Then
            converterTextBox2.Text = ""
        ElseIf sender.name = "converter3ComboBox" Then
            converterTextBox3.Text = ""
        End If

        imageBoxName.Visible = False
        sender.selectedIndex = -1
    End Sub

    Public Sub fillCurrensyTable()
        currensyTable(1, 1) = 1 'Euro to Euro
        currensyTable(1, 2) = 1.2468 'Euro to American Dollar
        currensyTable(1, 3) = 0.8079 'Euro to British Pound
        currensyTable(1, 4) = 98.9 'Euro to Japanese Yen
        currensyTable(1, 5) = 1.2665 'Euro to Australian Dollar
        currensyTable(1, 6) = 1.9558 'Euro to Bulgarian Lev
        currensyTable(1, 7) = 7.4319 'Euro to Danish Krone
        currensyTable(1, 8) = 1.2012 'Euro to Swiss Frank
        currensyTable(1, 9) = 2.2848 'Euro to Turkish Lira
        currensyTable(1, 10) = 5.5911 'Euro to Argentine Peso

        currensyTable(2, 1) = 0.80205 'American Dollar to Euro
        currensyTable(2, 2) = 1 'American Dollar to American Dollar
        currensyTable(2, 3) = 0.64802 'American Dollar to British Pound
        currensyTable(2, 4) = 79.3231 'American Dollar to Japanese Yen
        currensyTable(2, 5) = 1.0158 'American Dollar to Australian Dollar
        currensyTable(2, 6) = 1.5686 'American Dollar to Bulgarian Lev
        currensyTable(2, 7) = 5.96078 'American Dollar to Danish Krone
        currensyTable(2, 8) = 0.963426 'American Dollar to Swiss Frank
        currensyTable(2, 9) = 1.83253 'American Dollar to Turkish Lira
        currensyTable(2, 10) = 4.48438 'American Dollar to Argentine Peso

        currensyTable(3, 1) = 1.23769 'British Pound to Euro
        currensyTable(3, 2) = 1.54316 'British Pound to American Dollar
        currensyTable(3, 3) = 1 'British Pound to British Pound
        currensyTable(3, 4) = 122.408 'British Pound to Japanese Yen
        currensyTable(3, 5) = 1.56754 'British Pound to Australian Dollar
        currensyTable(3, 6) = 2.4207 'British Pound to Bulgarian Lev
        currensyTable(3, 7) = 9.19846 'British Pound to Danish Krone
        currensyTable(3, 8) = 1.48672 'British Pound to Swiss Frank
        currensyTable(3, 9) = 2.82789 'British Pound to Turkish Lira
        currensyTable(3, 10) = 6.92013 'British Pound to Argentine Peso

        currensyTable(4, 1) = 0.010111 'Japanese Yen to Euro
        currensyTable(4, 2) = 0.012606 'Japanese Yen to American Dollar
        currensyTable(4, 3) = 0.08169 'Japanese Yen to British Pound
        currensyTable(4, 4) = 1 'Japanese Yen to Japanese Yen
        currensyTable(4, 5) = 0.012805 'Japanese Yen to Australian Dollar
        currensyTable(4, 6) = 0.019775 'Japanese Yen to Bulgarian Lev
        currensyTable(4, 7) = 0.075145 'Japanese Yen to Danish Krone
        currensyTable(4, 8) = 0.012145 'Japanese Yen to Swiss Frank
        currensyTable(4, 9) = 0.023102 'Japanese Yen to Turkish Lira
        currensyTable(4, 10) = 0.056533 'Japanese Yen to Argentine Peso

        currensyTable(5, 1) = 0.789577 'Australian Dollar to Euro
        currensyTable(5, 2) = 0.984445 'Australian Dollar to American Dollar
        currensyTable(5, 3) = 0.637939 'Australian Dollar to British Pound
        currensyTable(5, 4) = 78.0892 'Australian Dollar to Japanese Yen
        currensyTable(5, 5) = 1 'Australian Dollar to Australian Dollar
        currensyTable(5, 6) = 1.54426 'Australian Dollar to Bulgarian Lev
        currensyTable(5, 7) = 5.86806 'Australian Dollar to Danish Krone
        currensyTable(5, 8) = 0.94844 'Australian Dollar to Swiss Frank
        currensyTable(5, 9) = 1.80402 'Australian Dollar to Turkish Lira
        currensyTable(5, 10) = 4.41462 'Australian Dollar to Argentine Peso

        currensyTable(6, 1) = 0.511298 'Bulgarian Lev to Euro
        currensyTable(6, 2) = 0.637486 'Bulgarian Lev to American Dollar
        currensyTable(6, 3) = 0.413103 'Bulgarian Lev to British Pound
        currensyTable(6, 4) = 50.5674 'Bulgarian Lev to Japanese Yen
        currensyTable(6, 5) = 0.647559 'Bulgarian Lev to Australian Dollar
        currensyTable(6, 6) = 1 'Bulgarian Lev to Bulgarian Lev
        currensyTable(6, 7) = 3.79991 'Bulgarian Lev to Danish Krone
        currensyTable(6, 8) = 0.614171 'Bulgarian Lev to Swiss Frank
        currensyTable(6, 9) = 1.16821 'Bulgarian Lev to Turkish Lira
        currensyTable(6, 10) = 2.85873 'Bulgarian Lev to Argentine Peso

        currensyTable(7, 1) = 0.134555 'Danish Krone to Euro
        currensyTable(7, 2) = 0.167763 'Danish Krone to American Dollar
        currensyTable(7, 3) = 0.108713 'Danish Krone to British Pound
        currensyTable(7, 4) = 13.3075 'Danish Krone to Japanese Yen
        currensyTable(7, 5) = 0.170413 'Danish Krone to Australian Dollar
        currensyTable(7, 6) = 0.263163 'Danish Krone to Bulgarian Lev
        currensyTable(7, 7) = 1 'Danish Krone to Danish Krone
        currensyTable(7, 8) = 0.161627 'Danish Krone to Swiss Frank
        currensyTable(7, 9) = 0.307431 'Danish Krone to Turkish Lira
        currensyTable(7, 10) = 0.752314 'Danish Krone to Argentine Peso

        currensyTable(8, 1) = 0.8325 'Swiss Frank to Euro
        currensyTable(8, 2) = 1.03796 'Swiss Frank to American Dollar
        currensyTable(8, 3) = 0.672619 'Swiss Frank to British Pound
        currensyTable(8, 4) = 82.3343 'Swiss Frank to Japanese Yen
        currensyTable(8, 5) = 1.05436 'Swiss Frank to Australian Dollar
        currensyTable(8, 6) = 1.62821 'Swiss Frank to Bulgarian Lev
        currensyTable(8, 7) = 6.18706 'Swiss Frank to Danish Krone
        currensyTable(8, 8) = 1 'Swiss Frank to Swiss Frank
        currensyTable(8, 9) = 1.90209 'Swiss Frank to Turkish Lira
        currensyTable(8, 10) = 4.65461 'Swiss Frank to Argentine Peso

        currensyTable(9, 1) = 0.437675 'Turkish Lira to Euro
        currensyTable(9, 2) = 0.545693 'Turkish Lira to American Dollar
        currensyTable(9, 3) = 0.353619 'Turkish Lira to British Pound
        currensyTable(9, 4) = 43.2861 'Turkish Lira to Japanese Yen
        currensyTable(9, 5) = 0.554315 'Turkish Lira to Australian Dollar
        currensyTable(9, 6) = 0.856007 'Turkish Lira to Bulgarian Lev
        currensyTable(9, 7) = 3.25275 'Turkish Lira to Danish Krone
        currensyTable(9, 8) = 0.525735 'Turkish Lira to Swiss Frank
        currensyTable(9, 9) = 1 'Turkish Lira to Turkish Lira
        currensyTable(9, 10) = 2.44709 'Turkish Lira to Argentine Peso

        currensyTable(10, 1) = 0.178854 'Argentine Peso to Euro
        currensyTable(10, 2) = 0.222996 'Argentine Peso to American Dollar
        currensyTable(10, 3) = 0.144505 'Argentine Peso to British Pound
        currensyTable(10, 4) = 17.6887 'Argentine Peso to Japanese Yen
        currensyTable(10, 5) = 0.226519 'Argentine Peso to Australian Dollar
        currensyTable(10, 6) = 0.349805 'Argentine Peso to Bulgarian Lev
        currensyTable(10, 7) = 1.32923 'Argentine Peso to Danish Krone
        currensyTable(10, 8) = 0.21484 'Argentine Peso to Swiss Frank
        currensyTable(10, 9) = 0.408647 'Argentine Peso to Turkish Lira
        currensyTable(10, 10) = 1 'Argentine Peso to Argentine Peso
    End Sub

    Private Sub infoBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles infoBtn.Click
        MessageBox.Show("Χριστίνα Βάτσου - Ελένη Πούνιου" & vbCrLf & vbCrLf & _
                        "- UserControl Εμφάνισης και υπολογισμού ισοτιμιών νομισμάτων -" & vbCrLf & _
                        "Developed in Microsoft Visual Studio 2008 with Visual Basic .NET", _
                                    "Currensy Converter", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class
